package com.hfb.mashibing.alip8.designpattern._09observer;

import java.util.LinkedList;
import java.util.List;
import lombok.Getter;
import lombok.ToString;

public class V9 {

    public static void main(String[] args) {
        Button b = new Button();
        b.addActionListener(new MyActionListenerV9());
        b.addActionListener(new MyActionListenerV92());
        b.addActionListener(e->
            System.out.println(
                "Observer、Listener、hook、callback function 都是一回事，都是指观察者模式（千万不要被迷惑！）！")
        );
        b.buttonPressed();
    }
}
class Button {
    private List<ActionListenerV9> actionListeners = new LinkedList<>();
    public void buttonPressed(){
        ActionEventV9 e = new ActionEventV9(System.currentTimeMillis(), this);
        actionListeners.forEach(l->{
            l.actionPerformed(e);
        });
    }
    public boolean addActionListener(ActionListenerV9 l){
        return actionListeners.add(l);
    }
}
interface ActionListenerV9 {
    void actionPerformed(ActionEventV9 e);
}
class MyActionListenerV9 implements ActionListenerV9{

    @Override
    public void actionPerformed(ActionEventV9 e) {
        System.out.println("button is pressed!");
    }
}
class MyActionListenerV92 implements ActionListenerV9{

    @Override
    public void actionPerformed(ActionEventV9 e) {
        System.out.println("button is pressed 2!");
    }
}
@Getter
@ToString
class ActionEventV9 {
    long when;
    Object source;

    public ActionEventV9(long when, Object source) {
        this.when = when;
        this.source = source;
    }
}
